﻿using Comm;
using GalaSoft.MvvmLight.Messaging;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.NetworkInformation;
using System.Net.Sockets;
using System.Text;
using System.Threading.Tasks;

namespace Kinlo.Drv.Drv
{
    public class KNCScannerDrv
    {
        Socket socket;
        string IP;
        int Port;
        string ID; 
        int RECV_DATA_MAX = 1024;
        public KNCScannerDrv(string ip, int port, string id, int timeout)
        {
            try
            {
                IP = ip;
                Port = port;
                ID = id;
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                SendTimeout = timeout;
                ReceiveTimeout = timeout;
                socket.SendTimeout = SendTimeout;
                socket.ReceiveTimeout = ReceiveTimeout;
                Logger.Info(string.Format("创建 {0}号扫码枪 {1}:{2} 完成", ID, IP, Port), false);

            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("创建 {0}号扫码枪 {1}:{2} 发生了异常:{3}", ID, IP, Port, ex.Message),false);
                throw ex;
            }
        }
        public void StartScan()
        {
            try
            {
                //if (!socket.Connected)
                //{
                //    Open();
                //}
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.SendTimeout = SendTimeout;
                socket.ReceiveTimeout = ReceiveTimeout;
                socket.Connect(IP, Port);

                string lon = "LON\r";   // CR is terminator
                Byte[] command = ASCIIEncoding.ASCII.GetBytes(lon);
                socket.Send(command);
                var msg = string.Format("发送 {0}号扫码枪 {1}:{2} 发送扫描命令完成", ID, IP, Port); 
                Logger.Info(msg, false);
            }
            catch (SocketException sex)
            {
                var msg = string.Format("启动 {0}号扫码枪 {1}:{2} 扫描时发生了异常1:{3}", ID, IP, Port, sex.Message); 
                Logger.Error(msg, false);
            }
            catch (Exception ex)
            {
                var msg = string.Format("启动 {0}号扫码枪 {1}:{2} 扫描时发生了异常:{3}", ID, IP, Port, ex.Message); 
                Logger.Error(msg, false);
                throw ex;
            }
        }

        public void Open()
        {
            try
            {
                socket = new Socket(AddressFamily.InterNetwork, SocketType.Stream, ProtocolType.Tcp);
                socket.SendTimeout = SendTimeout;
                socket.ReceiveTimeout = ReceiveTimeout;
                socket.Connect(IP, Port);

                Logger.Info(string.Format("连接 {0}号扫码枪 {1}:{2} 成功", ID, IP, Port), false);
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("打开 {0}号扫码枪 {1}:{2} 发生了异常:{3}", ID, IP, Port, ex.Message), false);
            }
        }
        public bool Connected
        {
            get
            {
                try
                {
                    Ping ping = new Ping();
                    var a = ping.Send(IP);
                    if (a.Status == IPStatus.Success && socket.Connected)
                    { 
                        return true;
                    }
                    else
                    { 
                        return false;
                    }
                }
                catch
                {
                    return false;
                }
            }
        }
        int receiveTimeout;
        public int ReceiveTimeout
        {
            get { return receiveTimeout; }
            set
            {
                receiveTimeout = value;
            }
        }

        int sendTimeout;
        public int SendTimeout
        {
            get { return sendTimeout; }
            set
            {
                sendTimeout = value;
            }
        }
        public string Receive()
        {
            try
            {
                Byte[] recvBytes = new Byte[RECV_DATA_MAX];
                if (socket.Connected)
                {
                    var len = socket.Receive(recvBytes);
                    if (len > 0)
                    {
                        var barcode = Encoding.Default.GetString(recvBytes, 0, len);
                        var msg1 = string.Format("读 {0}号扫码枪 {1}:{2} 完成 {3}", ID, IP, Port, barcode); 
                        Logger.Info(msg1, false);
                        return barcode;
                    }
                }
                var msg = string.Format("读 {0}号扫码枪 {1}:{2} 完成 1", ID, IP, Port); 
                Logger.Info(msg, false);
                return "";
            }
            catch (Exception ex)
            {
                var msg = string.Format("读 {0}号扫码枪 {1}:{2} 发生了异常:{3}", ID, IP, Port, ex.Message); 
                Logger.Error(msg);
                return "";
            }
            finally
            {
                StopScan();
            }
        }
        public void StopScan()
        {
            try
            {
                string loff = "LOFF\r"; // CR is terminator
                Byte[] command = ASCIIEncoding.ASCII.GetBytes(loff);
                socket.Send(command);
                socket.Close();

                Logger.Info(string.Format("关闭 {0}号扫码枪 {1}:{2}", ID, IP, Port), false);
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("关闭时 {0}号扫码枪 {1}:{2} 发生了异常:{3}", ID, IP, Port, ex.Message), false);
            }

        }

        public void Close()
        {
            try
            {
                socket.Close();
            }
            catch (Exception ex)
            {
                Logger.Error(string.Format("扫码器关闭异常:{0} {1} {2}", IP, Port, ex.Message), false);
            }
        }


    }

}
